package com.zatech.cgnci.project.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zatech.cgnci.project.base.enums.ResponseCodeEnum;
import com.zatech.cgnci.project.base.exception.GlobalException;
import com.zatech.cgnci.project.base.utils.BeanCopierUtils;
import com.zatech.cgnci.project.base.utils.VerifyUtils;
import com.zatech.cgnci.project.dto.CgnciClaimAlertInfoDTO;
import com.zatech.cgnci.project.entity.CgnciClaimAlertInfoDO;
import com.zatech.cgnci.project.mapper.CgnciClaimAlertInfoMapper;
import com.zatech.cgnci.project.service.CgnciClaimAlertInfoService;
import com.zatech.cgnci.project.vo.CgnciClaimAlertInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 理赔案件警示信息表 服务实现类
 * </p>
 *
 * @author Generator
 * @since 2024-12-23
 */
@Service
public class CgnciClaimAlertInfoServiceImpl extends ServiceImpl<CgnciClaimAlertInfoMapper, CgnciClaimAlertInfoDO> implements CgnciClaimAlertInfoService {

    @Autowired
    private CgnciClaimAlertInfoMapper mapper;

    @Override
    public List<CgnciClaimAlertInfoVO> getByBusinessNo(String businessNo, String businessScene) {
        List<CgnciClaimAlertInfoDTO> byBusinessNo = mapper.getByBusinessNo(businessNo, businessScene);
        List<CgnciClaimAlertInfoVO> alertInfoVOS = BeanCopierUtils.copyObjects(byBusinessNo, CgnciClaimAlertInfoVO.class);
        return alertInfoVOS;
    }

    @Override
    public void clearBeforeInsert(List<CgnciClaimAlertInfoDO> alertInfoList) {
        if (VerifyUtils.isEmpty(alertInfoList)){
            log.error("参数为空");
            throw new GlobalException(ResponseCodeEnum.PARAMS_ILLEGAL, "参数为空");
        }
        alertInfoList.forEach(alertInfoDO -> {
            checkParam(alertInfoDO);
        }
        );
        clearAlertInfoByBusinessNo(alertInfoList.get(0).getBusinessNo(),alertInfoList.get(0).getBusinessScene());
        insertAlertInfoList(alertInfoList);
    }

    /**
     * 添加提示信息、风险信息
     *
     */

    @Override
    public void insertAlertInfo(CgnciClaimAlertInfoDO alertInfoDO) {
        checkParam(alertInfoDO);
        mapper.insert(alertInfoDO);
    }

    @Override
    public void insertAlertInfoList(List<CgnciClaimAlertInfoDO> alertInfoList) {
        alertInfoList.forEach(alertInfo -> {
            checkParam(alertInfo);
        });
        this.saveBatch(alertInfoList);
    }


    /**
     * 清除提示信息/风险信息
     * @param businessNo 业务号
     */
    @Override
    public void clearAlertInfoByBusinessNo(String businessNo,String businessScene) {
        mapper.clearAlertInfoByBusinessNo(businessNo,businessScene);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void clearHistoryAndSave(CgnciClaimAlertInfoDO req) {
        checkParam(req);
        Assert.hasText(req.getBusinessSceneType(),"业务场景类型缺失。");
        getBaseMapper().clearAlertInfoThreeFactor(req.getBusinessNo(), req.getBusinessScene(), req.getBusinessSceneType());
        req.setCreateTime(new Date());
        getBaseMapper().insert(req);
    }


    //参数校验
    private void checkParam(CgnciClaimAlertInfoDO alertInfoDO) {
        if(VerifyUtils.isNull(alertInfoDO)){
            log.error("参数为空");
            throw new GlobalException(ResponseCodeEnum.PARAMS_ILLEGAL, "参数为空");
        }
        if (VerifyUtils.isNull(alertInfoDO.getBusinessNo())) {
            log.error("业务号不得为空");
            throw new GlobalException(ResponseCodeEnum.PARAMS_ILLEGAL, "业务号不得为空");
        }
        if (VerifyUtils.isNull(alertInfoDO.getAlertType())) {
            log.error("类型不得为空");
            throw new GlobalException(ResponseCodeEnum.PARAMS_ILLEGAL, "类型不得为空");
        }
        if(VerifyUtils.isNull(alertInfoDO.getBusinessScene())){
            log.error("业务场景不得为空");
            throw new GlobalException(ResponseCodeEnum.PARAMS_ILLEGAL, "业务场景不得为空");
        }
    }
}
